package com.nsgf.library.ui.workbenches;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.databinding.ActivityPatrolFeedBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.adapter.PatrolFeedListAdapter;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.widget.prckerview.TimePickerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 巡查动态
 */
public class PatrolFeedActivity extends BaseActivity {

    StateHolder mState;

    private ActivityPatrolFeedBinding mBinding;

    private int type = 0;//列表类型 0水量 1雨量
    private PatrolFeedListAdapter mAdapter;
    private List<LibraryInfo> mDatas = new ArrayList<>();

    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mState = getActivityScopeViewModel(StateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);

    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new PatrolFeedListAdapter(this);
        return new DataBindingConfig(R.layout.activity_patrol_feed, BR.vm, mState)
                .addBindingParam(BR.adapter, mAdapter)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.white));
        }
        getLifecycle().addObserver(mAccountRequester);
        mBinding = (ActivityPatrolFeedBinding) getBinding();
        mState.titleBar.set(getIntent().getStringExtra("title"));
        type = getIntent().getIntExtra("type", 0);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1);
        mState.endDate.set(ymd.format(calendar.getTime()));
        endTime = calendar;

        Calendar calendarStar = Calendar.getInstance();
        calendarStar.set(Calendar.MONTH, calendarStar.get(Calendar.MONTH) - 1);
        mState.startDate.set(ymd.format(calendarStar.getTime()));
        starTime = calendarStar;


        getDatas();
        mAccountRequester.getLibraryList().observe(this, new Observer<ResponseStatus<List<LibraryInfo>>>() {
            @Override
            public void onChanged(ResponseStatus<List<LibraryInfo>> dataResult) {

                if (dataResult.getResponseCode() == 200) {
                    mState.patrolFeed.set(dataResult.getData());

                } else {
                    showToast(dataResult.getMsg());
                }
                if (mState.patrolFeed.get().size() > 0) {
                    mBinding.notdata.setVisibility(View.GONE);
                    mBinding.recyclerView.setVisibility(View.VISIBLE);
                } else {
                    mBinding.notdata.setVisibility(View.VISIBLE);
                    mBinding.recyclerView.setVisibility(View.GONE);
                }
            }
        });


    }

    private static SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());


    private void getDatas() {

        mAccountRequester.damDynamicsStatistics(mState.startDate.get(), mState.endDate.get());
    }


    private Calendar starTime, endTime;

    public class ClickProxy {

        public void search() {
            Intent intent = new Intent();
            intent.putExtra("search", mState.searchContent.get());
            setResult(RESULT_OK, intent);
            finish();
        }


        public void screenStarTime() {

            boolean[] type = new boolean[]{true, true, true, false, false, false};//显示类型 默认全部显示

            TimePickerView mTimePickerView = new TimePickerView.Builder(PatrolFeedActivity.this, new TimePickerView.OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {
                    mState.startDate.set(ymd.format(date));

                    getDatas();
                    starTime.setTime(date);
                }
            }).isCyclic(false)
                    .setRangDate(null, null)
                    .setType(type)
                    .setTitleText("开始日期")
                    .setDate(starTime).build();

            mTimePickerView.show();


        }

        public void screenEndTime() {

            boolean[] type = new boolean[]{true, true, true, false, false, false};//显示类型 默认全部显示

            TimePickerView mTimePickerView = new TimePickerView.Builder(PatrolFeedActivity.this, new TimePickerView.OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {
                    mState.endDate.set(ymd.format(date));
                    getDatas();
                    endTime.setTime(date);
                }
            }).isCyclic(false)
                    .setRangDate(null, null)
                    .setType(type)
                    .setTitleText("结束日期")
                    .setDate(endTime).build();

            mTimePickerView.show();


        }

    }
}
